<template>
  <div>
    <div>

      <div style="margin-top: 50px;margin-left: 5%;display: inline-block">
        <h2>最新发布的图书</h2>
      </div>

      <div style="float: right;margin-right: 5%;margin-top: 60px">
          <MyButtonGroup :data-list="butList" @me-click="myClick"></MyButtonGroup>
      </div>

    </div>

    <div v-loading="loading"
         element-loading-text="数据正在加载..."
         style="margin-left: auto;margin-right: auto;width: 90%;">
      <div style="padding-left: 30px">
        <div v-for="item in newBookByTypeList" style="
      margin-left: 35px;margin-top: 30px;display: inline-block;
      height: max-content;width: max-content;">
          <router-link :to="{path:'/bookDetail',query:{id: item.bookId}}">
            <BookSingle :book-data="item"></BookSingle>
          </router-link>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>

import BookSingle from "../Slither/BookSingle.vue";
import MyButtonGroup from "../Slither/MyButtonGroup.vue";
import {ref} from "vue";
import axios from "../../axios/index.js";
import {ElMessage} from "element-plus";

let loading = ref(false)

let butListAndId = []

const butList = ref([])
let newBookByTypeList = ref([])
const getNewBookByType = (typeId)=>{
    loading.value = true
    newBookByTypeList.value = []
    axios.get('/book/type/first/' + typeId).then(res => {
    //console.log(res.data.data);
        newBookByTypeList.value = res.data.data;
        loading.value = false
    }).catch(error => {
        loading.value = false
        ElMessage.error(error)
    })
}

const myClick = (index)=>{
  //console.log("发射上来的 " + index)
  //console.log(butListAndId[index].typeId);
  getNewBookByType(butListAndId[index].typeId)
}

const getBookType = ()=>{
  axios.get('/type/first').then(res => {
    //console.log(res.data.data);
    res = res.data.data;
    butListAndId = res;
    let tempList = [];
    for (let i = 0; i < res.length; i++) {
      tempList.push(res[i].typeName)
    }
    butList.value = tempList;
    getNewBookByType(butListAndId[0].typeId);
  }).catch(error => {
    ElMessage.error(error)
  })
}
getBookType();

</script>

<style scoped>
a{
  text-decoration: none;
  color: black;
}
</style>
